package advanced.sorm;

import advanced.sorm.bean.Columninfo;
import advanced.sorm.bean.TableInfo;
import advanced.sorm.utils.JDBCUtils;
import advanced.sorm.utils.ReflectUtils;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MysqlQuery implements Query {

    @Override
    public int executeDML(String sql, Object[] params) {
        Connection conn = DBManager.getConn();
        int count = 0;
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            JDBCUtils.handlerParams(params, ps);
            count = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 将一个对象存储到数据库中,
     * 只存储不为空的数据
     *
     * @param o
     */
    @Override
    public void insert(Object o) {
        Class<?> clz = o.getClass();
        ArrayList<Object> params = new ArrayList<>();
        TableInfo tableInfo = TableContext.poClassTableMap.get(clz);
        StringBuilder sql = new StringBuilder("insert into " + tableInfo.getTname() + "(");
        Field[] fields = clz.getDeclaredFields();
        int countNotNullField = 0;//计算不为null的属性值
        for (Field field : fields) {
            String name = field.getName();
            Object fieldValue = ReflectUtils.invokeGet(name, o);
            if (fieldValue != null) {
                countNotNullField++;
                sql.append(name + ",");
                params.add(fieldValue);
            }
        }
        sql.setCharAt(sql.length() - 1, ')');
        sql.append("values (");
        for (int i = 0; i < countNotNullField; i++) {
            sql.append("?,");
        }
        sql.setCharAt(sql.length() - 1, ')');
        executeDML(sql.toString(), params.toArray());

    }

    @Override
    public void delete(Object o) {
        Class<?> clz = o.getClass();
        TableInfo tableInfo = TableContext.poClassTableMap.get(clz);
        Columninfo onlyPriKey = tableInfo.getOnlyPriKey();//主键的名字信息
        //通过 反射机制, 调用对应的get方法和set方法
        Object prikeyValue = ReflectUtils.invokeGet(onlyPriKey.getName(), o);
        delete(clz, prikeyValue);
    }

    @Override
    public int delete(Class clz, Object id) {
        TableInfo tableInfo = TableContext.poClassTableMap.get(clz);
        Columninfo onlyPriKey = tableInfo.getOnlyPriKey();
        String sql = "delete from " + tableInfo.getTname() + " where " + onlyPriKey.getName() + "=? ";
        executeDML(sql, new Object[]{id});
        return 0;
    }

    @Override
    public int update(Object obj, String[] filedNames) {
        return 0;
    }

    @Override
    public List queryRows(String sql, Class clz, Object[] params) {
        return null;
    }

    @Override
    public Object queryUniqueRow(String sql, Class clz, Object[] params) {
        return null;
    }

    @Override
    public Object queryValue(String sql, Object[] params) {
        return null;
    }

    @Override
    public Number queryNumber(String sql, Object[] params) {
        return null;
    }


}
